import mxit.aes

def text_array(text):
    length = 16
    text = "<mxit/>" + text
    
    ar = map(ord, text)
    while len(ar) < length -1:
        ar.append(0)
    ar.append(length - len(text))
    return ar
    
    
def key_array(key):
    key = map(ord, key)[:16]
    initial = map(ord, "6170383452343567")
    while len(key) < len(initial):
        key.append(initial[len(key)])
    return key
    
def client_id(jid):
    n = int(jid[:2], 16)
    return jid[2:2+n]
    
def encode_password(jid, password):
    import base64
    pass2 = text_array(password)
    n = int(jid[:2], 16)
    key = key_array(jid[2+n:])
    moo = mxit.aes.AESModeOfOperation()
    encoded = moo.aes.encrypt(pass2, key, moo.aes.keySize["SIZE_128"]) 
    b64 = base64.b64encode(''.join(map(chr, encoded)))
    return b64